<?php


namespace addon\hotel\model;

use addon\fenxiao\model\Config;
use app\model\BaseModel;
use app\model\member\MemberAccount;

class HotelFenxiaoOrder extends BaseModel
{
    /**
     * 分销订单计算
     * @param unknown $order
     */
    public function calculate($order)
    {
        //获取分销基础配置
        $config_model = new Config();
        $fenxiao_basic_config = $config_model->getFenxiaoBasicsConfig();

        $level_config = $fenxiao_basic_config[ 'data' ][ 'value' ][ 'level' ];
        $one_rate = $fenxiao_basic_config[ 'data' ][ 'value' ][ 'one_rate' ];
        $two_rate = $fenxiao_basic_config[ 'data' ][ 'value' ][ 'two_rate' ];
        $platform_rate = $fenxiao_basic_config[ 'data' ][ 'value' ][ 'platform_rate' ];
        if (empty($level_config)) {
            return $this->success();
        }

        //检测分销商上级关系
        $member_info = model("member")->getInfo([ [ 'member_id', '=', $order[ 'member_id' ] ] ], 'fenxiao_id');

        //如果没有分销商直接返回不计算,没有考虑首次付款上下级绑定
        if ($member_info[ 'fenxiao_id' ] == 0) {
            return $this->success();
        }
        $fenxiao_id = $member_info[ 'fenxiao_id' ];
        $fenxiao_info = model("fenxiao")->getInfo([ [ 'fenxiao_id', '=', $fenxiao_id ], [ 'is_delete', '=', 0 ] ]);
        if (empty($fenxiao_info)) {
            return $this->success();
        }

        //判断几级分销
        $parent_fenxiao_info = $level_config >= 2 ? model('fenxiao')->getInfo([ [ 'fenxiao_id', '=', $fenxiao_info[ 'parent' ] ], [ 'is_delete', '=', 0 ] ], 'fenxiao_id, fenxiao_name, status, parent') : [];
        $grand_parent_fenxiao_info = $level_config >= 3 && !empty($parent_fenxiao_info[ 'parent' ]) ? model('fenxiao')->getInfo([ [ 'fenxiao_id', '=', $parent_fenxiao_info[ 'parent' ] ], [ 'is_delete', '=', 0 ] ], 'fenxiao_id, fenxiao_name, status') : [];

        $fenxiao_info = $parent_fenxiao_info;
        $parent_fenxiao_info = $grand_parent_fenxiao_info;



        model('hotel_fenxiao_order')->delete([ [ 'order_id', '=', $order[ 'order_id' ] ] ]);
        //获取分销等级

        $commission = 0;
        $commission_rate = 0;
        $order_fenxiao_data = [];

        if ($fenxiao_info[ 'status' ] == 1) {
            $commission_rate = $one_rate;
            $commission += $order_fenxiao_data[ 'one_commission' ] = $one_rate * $order[ 'real_room_money' ] / 100;
        }
        if (!empty($parent_fenxiao_info) && $parent_fenxiao_info[ 'status' ] == 1) {
            $commission_rate = $two_rate;
            $commission += $order_fenxiao_data[ 'two_commission' ] = $two_rate * $order[ 'real_room_money' ] / 100;
        }

        //启动分销
        $data = [
            'order_id' => $order[ 'order_id' ],
            'order_no' => $order[ 'order_no' ],
            'hotel_id' => $order[ 'hotel_id' ],
            'hotel_name' => $order[ 'hotel_name' ],
            'room_id' => $order[ 'room_id' ],
            'room_name' => $order[ 'room_name' ],
            'room_image' => $order[ 'room_image' ],
            'price' => $order[ 'room_price' ],
            'num' => $order[ 'room_num' ],
            'real_room_money' => $order[ 'real_room_money' ],
            'member_id' => $order[ 'member_id' ],
            'member_name' => $order[ 'realname' ],
            'member_mobile' => $order[ 'mobile' ],
            'commission' => $commission,
            'commission_rate' => $commission_rate,
            'one_fenxiao_id' => $fenxiao_info[ 'fenxiao_id' ] ?? 0,
            'one_rate' => $one_rate,
            'one_commission' => $order_fenxiao_data[ 'one_commission' ] ?? 0,
            'one_fenxiao_name' => $fenxiao_info[ 'fenxiao_name' ] ?? '',
            'two_fenxiao_id' => !empty($parent_fenxiao_info) ? $parent_fenxiao_info[ 'fenxiao_id' ] : 0,
            'two_rate' => $two_rate,
            'two_commission' => empty($order_fenxiao_data[ 'two_commission' ]) ? 0 : $order_fenxiao_data[ 'two_commission' ],
            'two_fenxiao_name' => empty($parent_fenxiao_info) ? '' : $parent_fenxiao_info[ 'fenxiao_name' ],
            'create_time' => time()
        ];

        $res = model("hotel_fenxiao_order")->add($data);

        return $this->success();
    }

    /**
     * 订单退款
     * @param $order_id
     * @return array
     */
    public function refund($order_id)
    {
        $res = model("hotel_fenxiao_order")->update([ 'is_refund' => 1 ], [ [ 'order_id', '=', $order_id ] ]);
        return $this->success($res);
    }

    /**
     * 订单结算
     * @param $order_id
     */
    public function settlement($order_id)
    {
        //获取未退款的和未结算的分销订单
        $fenxiao_orders = model("hotel_fenxiao_order")->getList([ [ 'order_id', '=', $order_id ], [ 'is_settlement', '=', 0 ], [ 'is_refund', '=', 0 ] ], '*');
        //同时修改分销订单状态为已结算
        model("hotel_fenxiao_order")->startTrans();
        try {
            $member_account = new MemberAccount();
            model('hotel_fenxiao_order')->update([ 'is_settlement' => 1 ], [ [ 'order_id', '=', $order_id ] ]);
            $commission = 0;
            foreach ($fenxiao_orders as $fenxiao_order) {
                $one_fenxiao_info = model('fenxiao')->getInfo(['fenxiao_id'=>$fenxiao_order[ 'one_fenxiao_id' ]],'member_id');
                $one_member_info = model('member')->getInfo(['member_id'=>$one_fenxiao_info[ 'member_id' ]],'member_id');
                $commission += $fenxiao_order[ 'one_commission' ];
                $member_account->addMemberAccount($one_member_info[ 'member_id' ], 'balance_money', $fenxiao_order[ 'one_commission' ], 'hotel_order_one_commission',$order_id,'酒店订单一级分销佣金:'.$fenxiao_order[ 'one_commission' ]);

                if ($fenxiao_order[ 'two_commission' ] > 0) {
                    $two_fenxiao_info = model('fenxiao')->getInfo(['fenxiao_id'=>$fenxiao_order[ 'two_fenxiao_id' ]],'member_id');
                    $two_member_info = model('member')->getInfo(['member_id'=>$two_fenxiao_info[ 'member_id' ]],'member_id');
                    $commission += $fenxiao_order[ 'two_commission' ];
                    $member_account->addMemberAccount($two_member_info[ 'member_id' ],'balance_money', $fenxiao_order[ 'two_commission' ], 'hotel_order_two_commission',$order_id,'酒店订单二级分销佣金:'.$fenxiao_order[ 'two_commission' ]);
                }
            }
            //增加订单佣金结算
            model('hotel_order')->setInc([ [ 'order_id', '=', $order_id ] ], 'commission', $commission);
            model("hotel_fenxiao_order")->commit();
            return $this->success();
        } catch (\Exception $e) {
            model("hotel_fenxiao_order")->rollback();
            return $this->error($e->getMessage());
        }
    }

    /**
     * calculateOrder 计算对应分销商的第一次订单数量&&金额
     * @param $order_id
     */
    public function calculateOrder($order_id)
    {
        $fenxiao_order_info = model('hotel_fenxiao_order')->getFirstData([ [ 'order_id', '=', $order_id ] ], 'one_fenxiao_id');
        if (!empty($fenxiao_order_info)) {
            $one_commission_sum = model('hotel_fenxiao_order')->getSum([ [ 'order_id', '=', $order_id ] ], 'one_commission');
            model('fenxiao')->setInc([ [ 'fenxiao_id', '=', $fenxiao_order_info[ 'one_fenxiao_id' ] ] ], 'one_fenxiao_order_num');
            model('fenxiao')->setInc([ [ 'fenxiao_id', '=', $fenxiao_order_info[ 'one_fenxiao_id' ] ] ], 'one_fenxiao_order_money', $one_commission_sum);

        }
        return $this->success();
    }

    /**
     * 获取分销订单列表
     * @param unknown $condition
     * @param number $page
     * @param string $page_size
     * @param string $order
     */
    public function getFenxiaoOrderPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'fo.order_id DESC')
    {
        $field = '
        fo.fenxiao_order_id,fo.order_no,fo.hotel_id,fo.hotel_name,fo.room_id,fo.room_name,fo.room_image,fo.price,fo.num,fo.real_room_money,fo.member_name,
        fo.member_mobile,fo.one_fenxiao_name,fo.is_settlement,fo.commission,fo.is_refund,m.nickname,m.mobile,
        o.order_status_name,o.create_time,fo.one_fenxiao_id,fo.two_fenxiao_id,fo.one_commission,fo.two_commission
        ';

        $alias = 'fo';
        $join = [
            [
                'hotel_order o',
                'fo.order_id = o.order_id',
                'inner'
            ],
            [
                'member m',
                'fo.member_id = m.member_id',
                'inner'
            ]
        ];
        $list = model('hotel_fenxiao_order')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
        return $this->success($list);
    }

    /**
     * 查询订单信息
     * @param $condition
     * @param string $field
     * @return array
     */
    public function getFenxiaoOrderInfo($condition, $field = '*')
    {
        $fenxiao_order_info = model('hotel_fenxiao_order')->getInfo($condition, $field);
        return $this->success($fenxiao_order_info);
    }

    /**
     * 订单详情
     * @param $condition
     * @param string $field
     * @return array
     */
    public function getFenxiaoOrderDetail($condition, $field = '*')
    {
        $fenxiao_order_info = model('hotel_fenxiao_order')->getInfo($condition, $field);
        return $this->success($fenxiao_order_info);
    }

    /**
     * 获取分销订单数量
     * @param $condition
     */
    public function getFenxiaoOrderCount($condition)
    {
        $count = model('hotel_fenxiao_order')->getCount($condition);
        return $this->success($count);
    }
}